package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PStoreBo;
import com.ruoyi.system.domain.vo.PStoreVo;
import com.ruoyi.system.domain.PStore;
import com.ruoyi.system.mapper.PStoreMapper;
import com.ruoyi.system.service.IPStoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 门店自提Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@RequiredArgsConstructor
@Service
public class PStoreServiceImpl implements IPStoreService {

    private final PStoreMapper baseMapper;

    /**
     * 查询门店自提
     */
    @Override
    public PStoreVo queryById(Integer id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询门店自提列表
     */
    @Override
    public TableDataInfo<PStoreVo> queryPageList(PStoreBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStore> lqw = buildQueryWrapper(bo);
        Page<PStoreVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门店自提列表
     */
    @Override
    public List<PStoreVo> queryList(PStoreBo bo) {
        LambdaQueryWrapper<PStore> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStore> buildQueryWrapper(PStoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStore> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), PStore::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getIntroduction()), PStore::getIntroduction, bo.getIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PStore::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), PStore::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailedAddress()), PStore::getDetailedAddress, bo.getDetailedAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), PStore::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), PStore::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), PStore::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getValidTime()), PStore::getValidTime, bo.getValidTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDayTime()), PStore::getDayTime, bo.getDayTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsShow()), PStore::getIsShow, bo.getIsShow());
        return lqw;
    }

    /**
     * 新增门店自提
     */
    @Override
    public Boolean insertByBo(PStoreBo bo) {
        PStore add = BeanUtil.toBean(bo, PStore.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改门店自提
     */
    @Override
    public Boolean updateByBo(PStoreBo bo) {
        PStore update = BeanUtil.toBean(bo, PStore.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除门店自提
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
